package cn.xiaoyu.su.strategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 生成随机库位，短链码的前缀命中哪一个库
 */
public class ShardingDBConfig {

    /**
     * 存储数据库位置编号
     */
    private static final List<String> dbPrefixList = new ArrayList<>();

    //配置启用哪些库的前置
    static {
        dbPrefixList.add("0");
        dbPrefixList.add("1");
        dbPrefixList.add("a");
        //dbPrefixList.add("b");
        //dbPrefixList.add("b");
        //dbPrefixList.add("b");
    }

    /**
     * 获取随机前缀
     * @return
     */
    public static String getRandomDBPrefix(String code){

        int hashCode = code.hashCode();

        int index = Math.abs(hashCode) % dbPrefixList.size();

        return dbPrefixList.get(index);
    }

}
